package com.framework.utils.pay.youfupay.method;

import com.alibaba.fastjson.JSONObject;
import com.framework.entity.sys.SysBank;
import com.framework.entity.sys.SysPayment;
import com.framework.exception.RRException;
import com.framework.utils.pay.suixingfu.horfu.util.Base64Utils;
import com.framework.utils.pay.suixingfu.horfu.util.DESUtils;
import com.framework.utils.pay.suixingfu.horfu.util.JsonUtils;
import com.framework.utils.pay.suixingfu.horfu.util.RSAUtils;
import com.framework.utils.pay.youfupay.entity.ContractModle;
import com.framework.utils.pay.youfupay.entity.RequestMessage;
import com.framework.utils.pay.youfupay.util.Constant;
import com.framework.utils.pay.youfupay.util.HttpClientHelper;
import com.google.gson.JsonObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试
 *
 * @author Admin
 */
public class SignContractQueryMethod {

    //static String url = "http://api.clooudnext.com:8280/pctrans-business/clientBusiness/common.do";
    static String url = "http://api.youfupingtai.com/clientBusiness/common.do";
    //static String url = "http://apitest.youfupingtai.com/pctrans-business/clientBusiness/common.do";
    /**
     *  查询签约状态
     * @param payment
     * @param secretKey
     * @param bank
     * @param levyId
     * @param identity
     * @return 0等待1成功 2未查询到信息，重新签约
     */
    public static Integer signContractQuery(SysPayment payment, String secretKey, SysBank bank,String levyId,String identity) {
        RequestMessage rm = new RequestMessage();
        rm.setReqId("req" + System.currentTimeMillis());
        rm.setFunCode("6011");

        rm.setMerId(payment.getClientId());
        rm.setVersion("V1.0");
        String detailData = null;
        RequestMessage requestMessage=null;
        try {
            String json = getJson(bank,levyId,identity);
            byte[] bs = DESUtils.encrypt(json.getBytes(Constant.CHARSET), secretKey);
            String reqDataEncrypt = Base64Utils.encode(bs);
            rm.setReqData(reqDataEncrypt);
            rm.setSign(RSAUtils.sign(reqDataEncrypt, payment.getPrivateKey()));
            String reqStr = JsonUtils.toJson(rm);
            System.out.println(reqStr);
            Map<String, Object> map = new HashMap<>();
            map.put("reqJson", reqStr);
            String resData = HttpClientHelper.httpClientPost(url, map, Constant.CHARSET);
            System.out.println("resData:"+resData);

            requestMessage = JsonUtils.fromJson(resData, RequestMessage.class);

        } catch (Exception e) {

        }

        if(!Constant.SUCCESS.equals(requestMessage.getResCode())){
            throw new RRException("9000",requestMessage.getResMsg());
        }


        try {
            byte[] base64bs = Base64Utils.decode(requestMessage.getResData());
            byte[] debs = DESUtils.decrypt(base64bs, secretKey);
            detailData = new String(debs, Constant.CHARSET);
        }catch (Exception e){

        }

        Map<String, Object> map = JSONObject.parseObject(detailData, Map.class);
        System.out.println("detailData:"+map);

        return Integer.valueOf(map.get("state").toString());

    }

    private static String getJson(SysBank bank, String levyId, String identity) {
        ContractModle cm = new ContractModle();
        cm.setCardNo(bank.getReservedBankNo());
        cm.setIdCard(identity);
        cm.setMobile(bank.getPhone());
        cm.setName(bank.getCardName());
        cm.setLevyId(Long.valueOf(levyId));
        String json = JsonUtils.toJson(cm);
        System.out.println("JSON数据" + json);
        return json;
    }


}
